package controllers.vecino;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CuotaService;
import services.VecinoService;
import controllers.AbstractController;
import domain.Cuota;

@Controller
@RequestMapping("/cuota/vecino")
public class CuotaVecinoController extends AbstractController {
	// Services
	@Autowired
	private CuotaService cuotaService;

	@Autowired
	private VecinoService vecinoService;

	// Constructors

	public CuotaVecinoController() {
		super();
	}

	// Listing methods
	@RequestMapping(value = "/presidente/extraordinariasList", method = RequestMethod.GET)
	public ModelAndView listPresidenteExtraordinarias(
			@RequestParam int comunidadDeVecinosId) {
		ModelAndView result;
		Collection<Cuota> cuotas;

		cuotas = cuotaService
				.findCuotasExtraordinariaByComunidadDeVecinosAndByPresidente(comunidadDeVecinosId);

		result = new ModelAndView("cuota/extraordinariasList");
		result.addObject("requestURI",
				"cuota/vecino/presidente/extraordinariasList.do");
		result.addObject("cuotas", cuotas);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Cuota> cuotas;
		int principalId;
		principalId = vecinoService.findPrincipalId();

		cuotas = cuotaService.findCuotasByVecino();
		result = new ModelAndView("cuota/vecino/list");
		result.addObject("requestURI", "cuota/vecino/list.do");
		result.addObject("principalId", principalId);
		result.addObject("cuotas", cuotas);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/noPagadasList", method = RequestMethod.GET)
	public ModelAndView noPagadasList() {
		ModelAndView result;
		Collection<Cuota> cuotas;
		int principalId;
		principalId = vecinoService.findPrincipalId();

		cuotas = cuotaService.findCuotasNoPagadasByVecino();
		result = new ModelAndView("cuota/vecino/noPagadasList");
		result.addObject("requestURI", "cuota/vecino/noPagadasList.do");
		result.addObject("principalId", principalId);
		result.addObject("cuotas", cuotas);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	// Listing Presidente
	@RequestMapping(value = "/list2", method = RequestMethod.GET)
	public ModelAndView list2(@RequestParam int comunidadId) {
		ModelAndView result;
		Collection<Cuota> cuotas;
		int principalId;
		principalId = vecinoService.findPrincipalId();

		cuotas = cuotaService.findCuotasByComunidadDeVecinosPresidente(comunidadId);

		result = new ModelAndView("cuota/list");
		result.addObject("requestURI", "cuota/vecino/list.do");
		result.addObject("cuotas", cuotas);
		result.addObject("principalId", principalId);
		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ comunidadId);

		return result;
	}

	@RequestMapping(value = "/unpaidList", method = RequestMethod.GET)
	public ModelAndView unpaidList(@RequestParam int comunidadId) {
		ModelAndView result;
		Collection<Cuota> cuotas;
		int principalId;

		principalId = vecinoService.findPrincipalId();
		cuotas = cuotaService.findCuotasNoPagadasByComunidadPresidente(comunidadId);

		result = new ModelAndView("cuota/unpaidList");
		result.addObject("requestURI", "cuota/vecino/list.do");
		result.addObject("cuotas", cuotas);
		result.addObject("principalId", principalId);
		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ comunidadId);

		return result;
	}

	@RequestMapping(value = "/ordinariasList", method = RequestMethod.GET)
	public ModelAndView ordinariasList(@RequestParam int comunidadId) {
		ModelAndView result;
		Collection<Cuota> cuotas;
		int principalId;

		principalId = vecinoService.findPrincipalId();

		cuotas = cuotaService.findCuotasOrdinariasByComunidadPresidente(comunidadId);

		result = new ModelAndView("cuota/ordinariasList");
		result.addObject("requestURI", "cuota/vecino/list.do");
		result.addObject("cuotas", cuotas);
		result.addObject("principalId", principalId);

		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ comunidadId);

		return result;
	}

	@RequestMapping(value = "/extraordinariasList", method = RequestMethod.GET)
	public ModelAndView extraordinariasList(@RequestParam int comunidadId) {
		ModelAndView result;
		Collection<Cuota> cuotas;
		int principalId;

		principalId = vecinoService.findPrincipalId();

		cuotas = cuotaService.findCuotasExtraordinariaByComunidadDeVecinosAndByPresidente(comunidadId);

		result = new ModelAndView("cuota/extraordinariasList");
		result.addObject("requestURI", "cuota/vecino/extraordinariasList.do");
		result.addObject("cuotas", cuotas);
		result.addObject("principalId", principalId);
		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ comunidadId);

		return result;
	}

	// Pagar

	@RequestMapping(value = "/pagar", method = RequestMethod.GET)
	public ModelAndView pagar(@RequestParam int cuotaId) {
		ModelAndView result;

		try {
			if (cuotaId == -1) {
				Collection<Cuota> cuotas;
				int principalId;
				principalId = vecinoService.findPrincipalId();

				cuotas = cuotaService.findCuotasNoPagadasByVecino();
				result = new ModelAndView("cuota/vecino/noPagadasList");
				result.addObject("requestURI", "cuota/vecino/noPagadaList.do");
				result.addObject("cuotas", cuotas);
				result.addObject("backURI", "/welcome/index.do");
				result.addObject("principalId", principalId);
				result.addObject("message", "payCuota.commit.error");
			} else {
				cuotaService.pagarCuota(cuotaId);

				result = new ModelAndView(
						"redirect:/cuota/vecino/noPagadasList.do");
			}
		}

		catch (Throwable oops) {
			Collection<Cuota> cuotas;
			int principalId;
			principalId = vecinoService.findPrincipalId();

			cuotas = cuotaService.findCuotasNoPagadasByVecino();

			result = new ModelAndView("cuota/vecino/noPagadasList");

			result.addObject("requestURI", "cuota/vecino/noPagadaList.do");
			result.addObject("cuotas", cuotas);
			result.addObject("backURI", "/welcome/index.do");
			result.addObject("message", "payCuota.commit.error");

			result.addObject("principalId", principalId);

		}
		return result;

	}
}
